/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Communication;

import Wasa.Response;

/**
 *
 * @author gianmariospacagna
 */
public class Communicator {

    private CommInterface myATCommand;

    public Communicator(String port) {
        this.myATCommand = new CommInterface(port);
    }

    public Communicator() {
        this("/dev/tty.usbserial-A4UUYW6A");
    }

    private Response generateResponse(String responseStr) {
        int k = responseStr.indexOf("\n");
        if (k > 0) {
            responseStr = responseStr.substring(0, k - 1);
        }
        try {
            //System.out.println("String received: " + responseStr);
            float value = Float.parseFloat(responseStr);
            //System.out.println("Value of value: " + value);
            return new Response(value);



        } catch (NumberFormatException e) {
            //System.out.println(e);

            //System.out.println("!!!Invalid data value!!!");

            if (responseStr.startsWith("OK")) {
                return new Response("OK");
            } else {
                return new Response("ERROR");
            }
        }


    }

    public Response measureTemp() {
        String responseStr = myATCommand.sendATCommand("ATS204?");
        
        return generateResponse(responseStr);

    }
    
    public Response ok(){
        String responseStr = myATCommand.sendATCommand("AT");
        
        return generateResponse(responseStr); 
    }
    
    public Response measureAx() {
        String responseStr = myATCommand.sendATCommand("ATS2021?");
        return generateResponse(responseStr);

    }
    

    public Response measureLight() {
        String responseStr = myATCommand.sendATCommand("ATS203?");
        return generateResponse(responseStr);
    }
}
